;(function ($) {
    $(function () {
        $('#loginPage').removeClass('dn');
        var vm = new Vue({
            el: '#loginPage',
            data: function () {
                return {
                    current_tab: 1,
                    tel: '',
                    vercode: '',
                    error: {
                        show_error: false,
                        msg: ''
                    },
                    isCanGet: true,
                    countDownText: '59秒后重新获取验证码',
                    fail_msg: ''
                }
            },
            created: function () {
                var user = localStorage.getItem('_u_info_') ? JSON.parse(localStorage.getItem('_u_info_')) : null
                if (user) {
                    if (user.type == 1) {
                        location.href = './shop-list.html'
                    } else if (user.type == 2) {
                        location.href = './my-shop.html'
                    }
                }
            },
            computed: {
                show_tel_clean: function () {
                    return $.trim(this.tel).length > 0 ? true : false
                },
                show_code_clean: function () {
                    return $.trim(this.vercode).length > 0 ? true : false
                }
            },
            methods: {
                changeTab: function (index, callback) {
                    this.current_tab = index
                    callback && callback();
                },
                cleanTelHandler: function () {
                    this.tel = ''
                },
                cleanCodeHandler: function() {
                    this.vercode = ''
                },
                getCodeHandler: function (isTabOne) {
                    var _this = this
                    if (!_this.isMobile(_this.tel)) {
                        _this.error = {
                            show_error: true,
                            msg: '请输入正确的手机号'
                        }
                        setTimeout(function () {
                            _this.error = {
                                show_error: false,
                                msg: ''
                            }
                        }, 3000)
                        return false;
                    }
                    if (!_this.isCanGet) {
                        return false;
                    }
                    $.mAjax({
                        url: '/user/get-vercode',
                        data: {
                            phone: _this.tel
                        },
                        success: function (res) {
                            if (res.status == 1) {
                                this.vercode = ''
                                let sec = 60;
                                _this.isCanGet = false;

                                for (let i = 0; i <= 60; i++) {
                                    setTimeout(function () {
                                        if (sec != 0) {
                                            _this.isCanGet = false;
                                            _this.countDownText = sec + "秒后重新获取验证码"
                                            sec--;
                                        } else {
                                            sec = 60; //如果倒计时结束就让  获取验证码显示出来
                                            _this.isCanGet = true;
                                            _this.countDownText = "重新获取取验证码"
                                        }
                                    }, i * 1000)
                                }
                                if (isTabOne) {
                                    _this.changeTab(2);
                                }                                
                            } else {
                                _this.error = {
                                    show_error: true,
                                    msg: res.error.errormsg
                                }
                                setTimeout(function () {
                                    _this.error = {
                                        show_error: false,
                                        msg: ''
                                    }
                                }, 2000)
                            }
                        }
                    }, false)
                },
                countDown: function() {

                },
                // 判断是手机号码
                isMobile: function (tel) {
                    var r = /^((\(\d{3}\))|(\d{3}\-))?(13|14|15|16|17|18|19)\d{9}$/;
                    return r.test(tel.valueOf());
                },
                codeVer: function (code) {
                    if ($.trim(this.vercode).length == 0) {
                        return false
                    } else {
                        return true
                    }
                },
                bindHandler: function () {
                    var _this = this;
                    if (!_this.codeVer(_this.vercode)) {
                        _this.error = {
                            show_error: true,
                            msg: '请输入验证码'
                        }
                        setTimeout(function () {
                            _this.error = {
                                show_error: false,
                                msg: ''
                            }
                        }, 3000)
                        return false
                    }
                    $.mAjax({
                        url: '/user/login',
                        data: {
                            phone: _this.tel,
                            vercode: _this.vercode
                        },
                        success: function (res) {
                            if (res.status == 1) {
                                _this.changeTab(3, function () {
                                    if (res.data.type != 1 && res.data.type != 2) {
                                        _this.fail_msg = '您不是店长或者店主，请联系管理员'
                                    } else {
                                        localStorage.setItem('_u_info_', JSON.stringify(res.data))
                                        setTimeout(function () { // 在成功页停留3秒跳转到shop-list页
                                            if (res.data.type == 1) {
                                                location.href = './shop-list.html'
                                            } else if (res.data.type == 2) {
                                                location.href = './my-shop.html'
                                            }
                                        }, 2000)
                                    }
                                });

                            } else {
                                _this.error = {
                                    show_error: true,
                                    msg: res.error.errormsg
                                }
                                setTimeout(function () {
                                    _this.error = {
                                        show_error: false,
                                        msg: ''
                                    }
                                }, 3000)
                            }
                        }
                    }, false)
                },
                rebindHandler: function () {
                    this.current_tab = 1;
                    this.tel = '';
                    this.vercode = '';
                    this.fail_msg = ''
                    location.href = './index.html'
                }
            },
            filters: {
                hide: function (tel) {
                    return tel.substr(0, 3) + '****' + tel.substr(7, 11)
                }
            }
        })
    })
})(jQuery)